import React, { Component } from 'react'
import PropTypes from 'prop-types'

import "./index.css"

export default class Header extends Component {
  static propTypes={
    acceptTodo: PropTypes.func.isRequired
  }

  handleKeyUp=(e)=>{
    const code = e.keyCode
    if(code!==13) return

    const val = e.target.value.trim()
    if(val==='') {
      e.target.value = ''
      return
    }

    const {acceptTodo} = this.props
    acceptTodo(val)
    e.target.value = ''
  }

  render() {
    return (
      <div className="header">
        <input onKeyUp={this.handleKeyUp} type="text" placeholder="请输入你的任务名称，按回车键确认" />
      </div>
    )
  }
}
